package com.holly.unit.deform.modular.controller;

import com.alibaba.fastjson.JSONObject;
import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.deform.api.DeformFacadeApi;
import com.holly.unit.deform.api.pojo.facade.AddDeformDataRequest;
import com.holly.unit.deform.api.pojo.facade.EditDeformDataRequest;
import com.holly.unit.deform.api.pojo.facade.ListDeformDataRequest;
import com.holly.unit.deform.api.pojo.facade.QueryOrDelDeformDataRequest;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 类描述: 表单设计开放API
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/9 14:22
 */
@Slf4j
@RestController
@ApiResource(name = "表单设计")
@Api(tags = "表单设计")
public class DeformFacadeController {

    @Resource
    private DeformFacadeApi api;

    /**
     * 通过 desformCode 和 dataId 查询数据
     * @param desformCode
     * @param dataId
     * @return
     */
    @ApiOperation(value = "desformCode查询数据" ,notes = "desformCode查询数据")
    @GetResource(name = "desformCode查询数据" , path = "/designForm/getData/{desformCode}/{dataId}")
    public ResponseData apiGetData(@PathVariable("desformCode") String desformCode,
                                   @PathVariable("dataId") String dataId) {
        QueryOrDelDeformDataRequest request = new QueryOrDelDeformDataRequest();
        request.setDataId(dataId);
        request.setDesformCode(desformCode);
        return api.apiGetData(request);
    }

    /**
     * 通过 desformCode 新增数据
     *
     * @param desformCode
     * @param formData
     * @return
     */
    @ApiOperation(value = "新增数据" ,notes = "新增数据")
    @PostResource(name = "新增数据" , path = "/designForm/addData/{desformCode}")
    public ResponseData apiAddData(@PathVariable("desformCode") String desformCode, @RequestBody JSONObject formData) {
        AddDeformDataRequest request  = new AddDeformDataRequest();
        request.setDesformCode(desformCode);
        request.setFormData(formData);
        return api.apiAddData(request);
    }

    /**
     * 通过 desformCode 和 dataId 修改数据
     *
     * @param desformCode
     * @param dataId
     * @param formData
     * @return
     */
    @ApiOperation(value = "修改数据" ,notes = "修改数据")
    @PostResource(name = "修改数据" , path = "/designForm/editData/{desformCode}/{dataId}")
    public ResponseData apiEditData(@PathVariable("desformCode") String desformCode, @PathVariable("dataId") String dataId, @RequestBody JSONObject formData) {
        EditDeformDataRequest request = new EditDeformDataRequest();
        request.setDataId(dataId);
        request.setDesformCode(desformCode);
        request.setFormData(formData);
        return api.apiEditData(request);
    }

    /**
     * 通过 desformCode 和 dataId 删除数据
     *
     * @param desformCode
     * @param dataId
     * @return
     */
    @ApiOperation(value = "删除数据" ,notes = "删除数据")
    @PostResource(name = "删除数据" , path = "/designForm/deleteData/{desformCode}/{dataId}")
    public ResponseData apiDeleteData(@PathVariable("desformCode") String desformCode, @PathVariable("dataId") String dataId) {
        QueryOrDelDeformDataRequest request = new QueryOrDelDeformDataRequest();
        request.setDataId(dataId);
        request.setDesformCode(desformCode);
        request.setToken(LoginContext.me().getToken());
        return api.apiDeleteData(request);
    }

    /**
     * 通过 desformCode 和 dataId 查询数据
     *
     * @param desformCode
     * @return
     */
    @ApiOperation(value = "desformCode查询数据" ,notes = "desformCode查询数据")
    @GetResource(name = "desformCode查询数据" , path = "/designForm/getData/{desformCode}/list")
    public ResponseData apiGetDataList(@PathVariable("desformCode") String desformCode, ListDeformDataRequest request) {
        request.setDesformCode(desformCode);
        return api.apiGetDataList(request);
    }

}
